﻿using Greg.XmlEditor.Presentation.Presenters;

namespace Greg.XmlEditor.Presentation.Commands.Concrete
{
	public class CloseCommand : CommandBase
	{
		private readonly MasterPresenter presenter;

		public CloseCommand(MasterPresenter presenter)
		{
			this.presenter = presenter;
		}

		/// <summary>
		/// Refreshes the command state.
		/// </summary>
		public override void Refresh()
		{
			this.IsEnabled = this.presenter.CurrentDocument != null && this.presenter.CurrentDocument is DocumentPresenter;
		}

		/// <summary>
		/// Executes the command logics.
		/// </summary>
		public override void Invoke()
		{
			var temp = this.presenter.CurrentDocument;
			this.presenter.CurrentDocument = null;
			if (temp is DocumentPresenter)
				this.presenter.Documents.Remove((DocumentPresenter)temp);
		}
	}
}
